//
//  SAPForumCollectionViewCell.m
//  CarsHome
//
//  Created by dapeng on 15/12/7.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPForumCollectionViewCell.h"

@implementation SAPForumCollectionViewCell
- (void)dealloc {
    
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self tableViews];
        [self addSubview:self.tableView];
        [_tableView release];
        self.count = 1;
        _HUD = [MBProgressHUD showHUDAddedTo:self animated:YES];
        _HUD.color = [UIColor blackColor];
        _HUD.dimBackground = YES;
        _HUD.labelText = @"正在加载";
        [SAPNetWorkTool getWithUrl:forumChoice parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
            NSDictionary *dic = [result valueForKey:@"result"];
            self.forumOneModel = [[SAPForumOneModel alloc] initWithDataSource:dic];
            _HUD.hidden = YES;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        } fail:^(NSError *error) {
            NSLog(@"%@", error);
        }];
    }return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.tableView.frame = CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height - 108);
}
- (void)tableViews {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[SAPTForumableViewCell class] forCellReuseIdentifier:@"SAPTForumableViewCellIndentifier"];
    [self header];
    [self footer];
}

- (void)header {
    [_tableView addHeaderWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SAPNetWorkTool getWithUrl:forumChoice parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
                NSDictionary *dic = [result valueForKey:@"result"];
                self.forumOneModel = [[SAPForumOneModel alloc] initWithDataSource:dic];
                [_tableView reloadData];
                [_tableView headerEndRefreshing];
                _tableView.contentInset = UIEdgeInsetsMake(130, 0, 0, 0);
                
            } fail:^(NSError *error) {
                NSLog(@"%@", error);
            }];
            
        });
    }];
}
- (void)footer {
    [_tableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            _count++;
            
            NSString *string = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.5/club/jingxuantopic-pm1-c0-p%ld-s30.json", _count];
            [SAPNetWorkTool getWithUrl:string parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
                NSDictionary *dic = [result valueForKey:@"result"];
                SAPForumOneModel *oneModel = [[SAPForumOneModel alloc] initWithDataSource:dic];
                [self.forumOneModel.listArray addObjectsFromArray:oneModel.listArray];
                [_tableView reloadData];
                
                
                [_tableView footerEndRefreshing];
                
            } fail:^(NSError *error) {
                NSLog(@"%@", error);
            }];
            
        });
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAPTForumableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPTForumableViewCellIndentifier"];
    if (self.forumOneModel) {
        SAPForumTwoModel *twoModel = self.forumOneModel.listArray[indexPath.row];
        cell.twoModel = twoModel;
    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.forumOneModel.listArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_SIZE.height / 8;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    SAPForumTwoModel *twoModel = self.forumOneModel.listArray[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"http://comm.app.api.autohome.com.cn/static/sharejump.html?p=1&pm=1&go=1&cont=4&ourl=http://forum.app.autohome.com.cn/autov5.0.5/forum/club/topicsharedcontent-t%@-o0.html", twoModel.topicid];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
    [webView loadRequest:request];
    webView.scalesPageToFit = YES;
    webView.delegate = self;
    
    [self.delegate passWebView:webView];
    
    
}

@end
